package ${filePackageName};

import cn.hutool.core.util.StrUtil;
import ${beanCommonPackage}.EntityData;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

/**
 * 基础工具控制器
 *
 * @author ${projectAuthor}
 * @version 1.0
 * @date ${nowDate}
 */
@ApiIgnore
@Controller
public class BaseController {

    private Logger logger = LoggerFactory.getLogger(BaseController.class);

    /**
     * 初始化request
     */
    protected Map<String, Object> initRequestMap(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, String[]> params = request.getParameterMap();
        if (params != null) {
            for (Entry<String, String[]> entry : params.entrySet()) {
                String key = entry.getKey();
                String[] value = entry.getValue();
                if (StrUtil.isNotBlank(key)
                        && value != null && value.length > 0) {
                    if (StrUtil.isNotBlank(value[0])){
                        map.put(key, value[0]);
                    }
                }
            }
        }
        return map;
    }

    /**
     * @param response
     * @param data
     * @param contentType json格式： application/json;charset=UTF-8
     *                    html：text/html;charset=UTF-8
     */
    public void output(HttpServletResponse response, String data, String contentType) {
        try {
            response.setCharacterEncoding("utf-8");
            response.setContentType(contentType);
            PrintWriter out = response.getWriter();
            out.print(data);
            out.flush();
            out.close();
        } catch (IOException e) {
            logger.error("BaseController output error",e);
        }
    }

    /**
     * 获取EntityData
     * @param clazz
     * @param entityName
     * @param primaryKey
     * @return
     */
    protected EntityData getEntityData(Class clazz, String entityName, String primaryKey){
        EntityData entityData = new EntityData();
        entityData.setEntityName(entityName);
        entityData.setPrimaryKey(primaryKey);
        entityData.setAccessBaseUrl("${accessBaseUrl}");

        ApiModel apiModel = (ApiModel) clazz.getAnnotation(ApiModel.class);
        if(apiModel!=null){
            String entityComment = apiModel.value();
            entityData.setEntityComment(entityComment);
        }

        Map<String,Object> entityAttrs = new HashMap<>();
        Field[] fields = clazz.getDeclaredFields();
        for(Field field:fields){
            field.setAccessible(true);
            ApiModelProperty property = field.getAnnotation(ApiModelProperty.class);
            if(property!=null){
                String comment = property.value();
                String name = field.getName();
                entityAttrs.put(name,comment);
            }
        }
        entityData.setEntityAttrs(entityAttrs);
        return entityData;
    }
}
